<?php
/*
 * 项目更新配置，注册相关操作
 * 数据库更新
 */
class UpdateTask extends Phalcon\CLI\Task
{
    var $APP = 'base';
    var $TABLENAME = '';
    public function mainAction(){
       //更新配置文件
        $mydir = dir(APP_PATH. '/App/');
        //_p($mydir,false);
        var_dump($mydir);
        
        $config_arr = array();
	while($app = $mydir->read()){
//            if (is_readable(APP_PATH . '/app/'.$app.'/config.php')) {
//                $config_arr[$app] = include APP_PATH . '/app/'.$app.'/config.php';
//            }
            var_dump($app);
            if (is_dir(APP_PATH . '/App/'.$app.'/DBSchema/')) {
                            //数据库相关
                $this->updateSql($app,APP_PATH . '/App/'.$app.'/DBSchema/');
            }

	}
	$mydir->close();
//        //处理配置文件
//        $main_config['database'] = include APP_PATH . '/config/database.php';
//        foreach ($config_arr as $app => $v) {
//              if($v['enable'] == '1'){//启用
//                  $main_config[$app] = $v;
//                  
//              }  
//        }
//        $filename = APP_PATH . '/data/config/config.php';
//        $this->createFile($filename, $main_config);


    } 
    /*
     * 生成文件
     */
    public function createFile($filename,$data){
        file_put_contents($filename, serialize($data));  
    }

    
    /*
     * 数据库文件更新
     */
    public function updateSql($app,$app_path){
        //_p($app_path);
        $mydir = dir($app_path);
        //_p($mydir);
        var_dump($app_path);
        $config_arr = array();
        $prefix = 'system';
        $object_kvstore = new Base\Lib\Kvstore($prefix);
        //var_dump($object_kvstore);
	while($file = $mydir->read()){
             
            if ($file != '.' && $file!= '..') {
                //获取文件的md5_file(),对比记录的值，看是否需要update
                 $last_modified = last_modified_file($app_path.$file);
                 //var_dump($last_modified);
                 //var_dump($file);
                 $this->TABLENAME = basename($file,".php");
                 //service_last_modified.base_application_dbtable.base
                 $key = 'service_last_modified.'.$app.'_application_dbtable.'.$this->TABLENAME;
                 $old_last_modified = $object_kvstore->fetch($key);
                 //var_dump($old_last_modified);
                 //break;
                 if($last_modified != $old_last_modified){
                     //
                     //更新数据库
                     $DBTable = new Base\Lib\Application\DBTable($this->TABLENAME,$app);
                     $res = $DBTable->update();
                     $object_kvstore->store($key,$last_modified);
                 }else{
                    // var_dump($old_last_modified);
                 }
                 //exit();
//                if (is_readable($app_path.$file)) {
//                    $this->TABLENAME = basename($app_path.$file,".php");
//                    //_p($this->TABLENAME);
//                    $DBTable = new Base\Lib\Application\dbtable($this->TABLENAME,$this->APP);
//                    $res = $DBTable->install();
//                    //_p($res);
//                }
                
            }
	}
	$mydir->close();   
    }    
}